package com.zhang.util;

import com.zhang.exception.ServiceException;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.zhang.common.Constant.TIME_FORMAT;
import static com.zhang.common.Constant.TIME_FORMAT_ERROR;

/**
 * User: 张
 * Date: 2024-11-08
 * Time: 11:05
 * Description:
 */
@Component
public class TimeUtil {

    // 检查时间格式并进行转换
    public Date checkTimeFormat(String timeStr) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT);
        Date time = null;
        if (timeStr != null) {
            try {
                time = dateFormat.parse(timeStr);
            } catch (ParseException e) {
                throw new ServiceException(TIME_FORMAT_ERROR);
            }
        }
        return time;
    }
}
